//
//  HMSportTracking.m
//  BlackHorseLine
//
//  Created by made on 17/3/5.
//  Copyright © 2017年 made. All rights reserved.
//

#import "HMSportTracking.h"

@interface HMSportTracking ()


@end

@implementation HMSportTracking

- (instancetype)initWithSportType:(HMSportType)type
{
    return [self initWithSportType:type SportState:HMSportStateContinue];
}


- (instancetype)initWithSportType:(HMSportType)type SportState:(HMSportState)state
{
    self = [super init];
    _sportType = type;
    _sportState = state;
    self.trackingLineArr = [NSMutableArray array];
    return self;
    
}


- (void) setSportState:(HMSportState)sportState
{
    //1.赋值运动状态
    _sportState = sportState;
    
    //2.如果点击暂停,则清除轨迹起始点
    if (self.sportState == HMSportStatePause)
    {
        self.sportStartLocation = nil;
    }
}

//返回GPS信号
- (HMSportGPSSignalState )gpsStatewithLocation:(CLLocation *)location
{
    HMSportGPSSignalState state;
    NSLog(@"%f",location.horizontalAccuracy);
    //1.通过location的水平精度来判断gps信号
    if (location.horizontalAccuracy < 0) {
        state = HMSportGPSSignalStateDisconnect;
    } else if (location.horizontalAccuracy > 163) {
        state = HMSportGPSSignalStateStateBad;
    } else if (location.horizontalAccuracy > 48) {
        state = HMSportGPSSignalStateStateNormal;
    } else {
        state = HMSportGPSSignalStateStateGood;
    }
    //判断GPS信号是否发生变化,如果发生变化就通知告知外部,如果未发生变化则不告知(节省性能)
    if (_gpsSignalState == state)//信号没有任何变化
    {
        return state;
    }
    
    //保存当前gps信号
    _gpsSignalState = state;
    
    //发送通知告知外部GPS信号发生变更
    [[NSNotificationCenter defaultCenter] postNotificationName:kHMSportGPSSignalStateChange object:nil userInfo:@{@"key":@(state)}];
    
    return state;
}




- (UIImage *)sportImage
{
    UIImage *image;
    switch (self.sportType)
    {
        case HMSportTypeWalk:
            image = [UIImage imageNamed:@"map_annotation_walk"];
            break;
        case HMSportTypeRun:
            image = [UIImage imageNamed:@"map_annotation_run"];
            break;case HMSportTypeBike:
            image = [UIImage imageNamed:@"map_annotation_bike"];
            break;
            
        default:
            break;
    }
    return image;
}


-(HMSportPolyLine *)drawPolylineWithLocation:(CLLocation *)location
{
    
    //检测GPS信号
    NSLog(@"%f",location.horizontalAccuracy);
    
    
    //GPS信号优化算法
    //在实际开发中,如果GPS信号很差的情况下,一半APP有三种处理方式
    //1.不做任何处理,宁愿某一小段不给轨迹也不要给错误  2.当GPS信号很差的情况下,将信号保存起来做一个滤波算法优化,对算法工程师要求很高 3.当GPS信号很差或者无,利用硬件自身的传感器进行位置插补,对硬件要求比较
    if ( [self gpsStatewithLocation:location] <= HMSportGPSSignalStateStateBad)
    {
        return nil;
    }

    
    //检测经纬度的速度
    //NSLog(@"=======%f",location.speed);
    
    //判断速度是否小于0,如果小于0则认为当前用户处于室内或者静止状态,此时不应该记录轨迹(极大的提高APP的性能)
    if (location.speed <= 0)
    {
        return nil;
    }
    
//    //定义一个时间差因子,如果时间差超过2s我们认为该位置误差太大无效
//    if ([[NSDate date] timeIntervalSinceDate:location.timestamp] >= 2)
//    {
//        return nil;
//    }
//    
    
    //如果运动状态不是继续状态,则不绘制轨迹
    if (self.sportState != HMSportStateContinue)
    {
        return nil;
    }
    
    
    //1.判断是否存在运动起点,如果不存在运动起点则把当前位置作为运动起点(一个点无法绘制轨迹)
    if (self.sportStartLocation == nil)
    {
        self.sportStartLocation = location;
        return nil;
    }

    //2.创建轨迹线条模型
    //将参数位置作为本次线条的终点
    HMSportTrackingLine *trackline = [[HMSportTrackingLine alloc]initWithStartLocation:self.sportStartLocation EndLocation:location];
    
    //3.将本次折线的终点作为下一次折线的起始点
    self.sportStartLocation = location;
    
    //添加到轨迹折线模型数组
    [self.trackingLineArr addObject:trackline];
    NSLog(@"运动总时间 :%@ \n 运动的总距离:%.2f \n 运动的平均速度:%.2f km/h  \n 运动最大速度:%.2f",self.totaTimeStr,self.totalDistance,self.avgSpeed,self.maxSpeed);

    
    //4.返回折线模型
    return trackline.polyline;
}


- (CLLocation *)startLocation
{
    return self.sportStartLocation;
} 


//运动总时间
- (double)totalTime
{
    //数组KVC返回的数据类型是NSDecimalNumber(NSNumber的子类)
    id totaltime = [self.trackingLineArr valueForKeyPath:@"@sum.time"];
    return [totaltime doubleValue];
}

//运动总距离
- (double)totalDistance
{
    //数组KVC返回的数据类型是NSDecimalNumber(NSNumber的子类)
    id totaldistance = [self.trackingLineArr valueForKeyPath:@"@sum.distance"];
    return [totaldistance doubleValue];
}


//运动平均速度
- (double)avgSpeed
{
    //数组KVC返回的数据类型是NSDecimalNumber(NSNumber的子类)
    id avgspeed = [self.trackingLineArr valueForKeyPath:@"@avg.speed"];
    return [avgspeed doubleValue];
}

//运动最大速度
- (double)maxSpeed
{
    //数组KVC返回的数据类型是NSDecimalNumber(NSNumber的子类)
    id maxspeed = [self.trackingLineArr valueForKeyPath:@"@max.speed"];
    return [maxspeed doubleValue];
}


//运栋总时间字符串
- (NSString *)totaTimeStr
{
    //浮点型不能求模要转为int类型
    int totaltime = (int)self.totalTime;
    //把s转换为 00:00:00
    return [NSString stringWithFormat:@"%02zd:%02zd:%02zd",totaltime/3600,totaltime%3600/60,totaltime%60];
}



@end
